/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *
 */
package org.reliablesource.client.ui.viewers;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.events.TreeListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;

/**
 * 
 * ElementTableViewer.java<br>
 * Created on: Apr 19, 2007 <br>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
abstract public class ElementTableViewer extends ReliableViewer {

    protected Map<String, TreeColumn> COLUMN_MAP;

	public ElementTableViewer() {
		super();
	}

	@Override
	public void createViewerControl(Composite _parent) {
		contentViewer = new TreeViewer(_parent, SWT.FULL_SELECTION
				| SWT.H_SCROLL | SWT.V_SCROLL);
		Tree _content = ((TreeViewer) contentViewer).getTree();
		_content.addTreeListener(new TreeListener() {

			public void treeCollapsed(TreeEvent e) {
				refresh();
			}

			public void treeExpanded(TreeEvent e) {
				refresh();
			}

		});

		_content.setHeaderVisible(true);
		_content.setLinesVisible(true);

		String[] _columns = getColumns();
		int _nCols = _columns.length;
		for (int i = 0; i < _nCols; i++) {
			TreeColumn _tblCol = new TreeColumn(_content, SWT.CENTER);
			_tblCol.setText(_columns[i]);
			COLUMN_MAP.put(_columns[i], _tblCol);
			_tblCol.pack();
		}
		
		contentComposite = _content;
		contentComposite.pack();
	}
	
        @Override
        public void refresh() {
                contentComposite.setRedraw(false);
                Iterator<String> _it = COLUMN_MAP.keySet().iterator();
                while (_it.hasNext()) 
                        COLUMN_MAP.get(_it.next()).pack();
                contentComposite.layout();
                contentComposite.setRedraw(true);
            super.refresh();
        }
        

	abstract public String[] getColumns();

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.reliablesource.client.ui.viewers.ReliableViewer#init()
	 */
	@Override
	public void init() {
		super.init();
		COLUMN_MAP = new HashMap<String, TreeColumn>();
	}
}
